var util = require("../../../utils/util.js"),
    {
        api
    } = require("../../../utils/api.js"),
    app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cartList: [],
        selectGoods: [],
        isEdit: false,
        isAll: false,
        totalMoney: 0,
        isLoad:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options){
        this.cartList()
    },
    onShow() {
        if(this.data.isLoad){
            this.cartList(2);
        }
    },
    async cartList(flag=1) {
        let {
            data
        } = await api.cartList();
        let list=data && Array.isArray(data.goods_list) ? data.goods_list : [];
        if(flag == 2){
            list.forEach(item=>{
                let isAdd=true;
                this.data.cartList.forEach(val=>{
                    if(item.id === val.id)isAdd=false;
                });
                if(isAdd){
                    this.data.cartList.push(item);
                    this.setData({cartList:this.data.cartList})
                }
            })
        }else{
            this.setData({
                cartList: list,
                isLoad:true
            })
        }
        this.getAllMoney()
    },
    jian(e) {

    },
    removeSelect(e) {
        let index = e.currentTarget.dataset.index;
        let cartList = this.data.cartList;
        cartList[index].active = false;
        this.setData({
            cartList
        });
        this.getAllMoney()
    },
    addSelect(e) {
        let index = e.currentTarget.dataset.index;
        let cartList = this.data.cartList;
        cartList[index].active = true;
        this.setData({
            cartList
        });
        this.getAllMoney()
    },
    getAllMoney() {
        let cartList = this.data.cartList,
            totals = [],
            total = 0;
        cartList.forEach(item => {
            if (item.active) {
                let goods_num = item.goods_num ? item.goods_num : 1;
                totals.push((item.goods.price || 0) * goods_num)
            }
        });
        if (totals.length > 0) {
            totals.forEach(v => {
                total += v;
            });
        }
        this.setData({
            totalMoney: total.toFixed(2),
            isAll: totals.length == cartList.length ? true : false,
            selectGoods: totals
        })
    },
    addAllSelect() {
        let cartList = this.data.cartList;
        cartList.forEach(item => {
            item.active = true;
        });
        this.setData({
            cartList
        });
        this.getAllMoney();
    },
    removeAllSelect() {
        let cartList = this.data.cartList;
        cartList.forEach(item => {
            item.active = false;
        });
        this.setData({
            cartList
        });
        this.getAllMoney();
    },
    edit() {
        this.removeAllSelect();
        this.setData({
            isEdit: !this.data.isEdit
        })
    },
    settlement() {
        let ids = [],
            goods = [];
        console.log(' this.data.cartList', this.data.cartList);
        this.data.cartList.forEach(item => {
            if (item.active) {
                ids.push(item.id);
                goods.push(item.goods);
            }
        });
        console.log('goods', goods);
        wx.setStorageSync('orderGoodsList', goods)
        wx.navigateTo({
            url: `/pages/shopping/goodsSubmit/goodsSubmit?goods_type=1&cart_id=${ids.join(',')}`,
        })
    },
    delCart() {
        const ids = this.data.cartList.filter(o => o.active == true).map(v => v.id);
        wx.showModal({
            title: '提示',
            content: '确定要删除购物车商品?',
            complete: (res) => {
                if (res.confirm) {
                    api.delCart(ids.join(',')).then(res => {
                        app.showError('操作成功')
                        this.setData({
                            isEdit: false
                        })
                        this.cartList();
                    })
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage(e) {
        return util.shareEvent(e)
      }
})